<template>
  <div class="login_con">
    <h1 class="zz">铁路客车智慧检修管理系统</h1>
    <div class="login_box">
      <div class="ava_box">
        <img src="../assets/logo1.png" />
      </div>

      <el-form
        :model="login"
        :rules="loginRule"
        ref="loginRef"
        label-width="80px"
        class="login_form"
      >
        <el-form-item prop="account" label="劳资号：">
          <el-input prefix-icon="el-icon-s-custom" v-model="login.account"></el-input>
        </el-form-item>
        <el-form-item prop="password" label="密码：">
          <el-input
            prefix-icon="el-icon-unlock"
            v-model="login.password"
            type="password"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item class="login_btn">
          <el-button type="primary" @click="submitLogin">登录</el-button>
          <el-button @click="resRef">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      //登陆表单的数据绑定对象
      login: {
        account: "0000000001",
        password: "0000000001",
      },
      //表单的验证规则对象
      loginRule: {
        //验证用户名是否合法
        account: [
          { required: true, message: "请输用户名", trigger: "blur" },
          {
            min: 10,
            max: 10,
            message: "长度为10个字符",
            trigger: "blur",
          },
        ],
        //验证密码是否合法
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 10,
            max: 10,
            message: "长度为10个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    //重置
    resRef() {
      //console.log(this)
      this.$refs.loginRef.resetFields();
    },
    //表单登录预验证
    submitLogin() {
      this.$refs.loginRef.validate(async (valid) => {
        //console.log(valid)
        if (!valid) {
          return;
        }
        const result = await this.$http.post("cld/login", this.login);
        // console.log(result);
        //debugger
        if (result.status !== 200 || result.data.code !== 1) {
          return this.$message.error("登陆失败！");
        }
        //1.登录之后的token保存在 sessionStorage会话期间的存储机制    localStorage 是持久化的存储机制
        //2.编程 导航跳转到后台
        window.sessionStorage.setItem("token", result.data.data.token);
        // console.log(window.sessionStorage.getItem("token"));
        this.$message.success("登陆成功！");
        this.$router.push({
          name: "SelectionSystem",
          query: { type: result.data.data.type },
        });
        //console.log(result)
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login_con {
  background-image: url("../assets/bg.jpg");
  background-size: 100% 100%;
  height: 100%;
}
.zz {
  width: 100%;
  font-size: 42px;
  position: absolute;
  left: 0;
  top: 17.5%;
  color: #21a2a7;
  text-align: center;
  text-shadow: 5px 2px 4px rgb(139, 139, 139);
}
.login_box {
  width: 500px;
  height: 300px;
  border-radius: 3px;
  background-color: rgba(145, 142, 142, 0.2);
  position: absolute;
  left: 50%;
  top: 60%;
  transform: translate(-50%, -50%);
  transition: all 0.8s;
  //   box-shadow: 1px 1px 1px 1px #000000;
}

.ava_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  //   box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: rgba(145, 142, 142, 0.2);
  transition: all 0.8s;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
  }
}
.login_box:hover {
  box-shadow: 0 0 10px #6d7275;
}

.login_box:hover .ava_box {
  box-shadow: 0 0 10px #24a1a7;
  background-color: rgba(145, 142, 142, 0.5);
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.login_btn {
  display: flex;
  justify-content: flex-end;
}
</style>